package mapViewer.screens;

import context.*;

import mapViewer.*;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import java.util.Enumeration;


// ******************************************************************
// *** CLASS: MapScheetScreen
// ******************************************************************
public class MapSheetScreen extends List implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG

  private static final Command okCommand=new Command("OK",Command.ITEM,1);
  private static final Command exitCommand=new Command("Exit",Command.EXIT,1);
  private static final Command cancelCommand=new Command("Cancel",Command.ITEM,1);

  private static final Command newCommand=new Command("New",Command.ITEM,1);
  private static final Command deleteCommand=new Command("Delete",Command.ITEM,1);
	
  private Displayable prev;

  // ****************************************************************
  // *** CONSTRUCTOR: MapSheetScreen
  // ****************************************************************
  public MapSheetScreen(String title,final MapNavigator mapNavigator)
  { super(title,List.IMPLICIT);
    if(DEBUG) Debug.println("new MapSheetScreen:"+title);
    addCommand(okCommand);
    addCommand(newCommand);
    addCommand(deleteCommand);
    addCommand(cancelCommand);
    addCommand(exitCommand);
    setCommandListener(new CommandListener()
    { public void commandAction(Command c,Displayable s)
      { if(DEBUG) Debug.println("MapSheetScreen'commandAction:"+c+", item="+s);
        if(c==okCommand || c==SELECT_COMMAND)
        { int n=getSelectedIndex();
          MapSheet mapSheet=MapSheet.get(getString(n));
          MapNavigator.setCurrentMapLoader(mapSheet.getMapLoader());
          MapSheet.setSelectedMapSheet(mapSheet.getTitle());
          mapNavigator.startLoadMapThread();
          try { AccountRecordStore.storeAll(); } catch(Exception e) {}
          Context.setCurrent(prev);
        }
        else if(c==newCommand) new AddMapSheetScreen("Add MapSheet").show();
        else if(c==deleteCommand) deleteMapSheetCommand();//(item);
        else if(c==exitCommand) Context.exit();
        else if(c==cancelCommand) Context.setCurrent(prev);
      }
    });
	update();
  }

  private void appendMapSheetList()
  { if(DEBUG) Debug.println("MapSheetScreen.appendMapSheetList:");
	int n=0;
    for(Enumeration e=MapSheet.getMapSheetSet().elements();e.hasMoreElements();)
    { MapSheet sheet=(MapSheet)e.nextElement();
      append(sheet.getTitle(),null); n++;
      if(sheet.getMapLoader()==MapNavigator.getCurrentMapLoader()) setSelectedIndex(n-1,true);
    }
  }
  
  private void deleteMapSheetCommand()
  { final int x=getSelectedIndex();
    String sheetID=getString(x);
    if(DEBUG) Debug.println("MapSheetScreen.deleteMapSheetCommand: Selected sheet: "+sheetID);
    MapSheet.remove(sheetID);
    update(); 
    try { AccountRecordStore.storeAll(); } catch(Exception e) {}
  }

  // ******************************************************************
  // *** METHOD: show
  // ******************************************************************
  public void show() { prev=Context.setCurrent(this); }
  
  
  public void update()
  { deleteAll();
	appendMapSheetList();
  }

}